import java.util.*;

class Solution1 {
    HashMap<Character, Set<Character>> edges = new HashMap<>(); //邻接表
    HashMap<Character, Integer> in = new HashMap<>(); //每个顶点的入度
    boolean check = false;

    public String alienOrder(String[] words) {
        //初始化入度表
        for (String word : words) {
            for (int i = 0; i < word.length(); i++) {
                in.put(word.charAt(i), 0);
            }
        }

        //建图
        int n = words.length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                add(words[i], words[j]);
                if (check == true) return "";
            }
        }

        Queue<Character> queue = new LinkedList<>();
        for (Map.Entry<Character, Integer> entry : in.entrySet()) {
            if (entry.getValue() == 0) {
                queue.add(entry.getKey());
            }
        }

        StringBuffer ret = new StringBuffer();
        while (!queue.isEmpty()) {
            char t = queue.poll();
            ret.append(t);
            //更新相邻顶点的入度值
            for (char ch : edges.getOrDefault(t, new HashSet<>())) {
                in.put(ch, in.get(ch) - 1);
                if (in.get(ch) == 0) {
                    queue.add(ch);
                }
            }
        }

        for (char ch : in.keySet()) {
            if (in.get(ch) != 0) {
                return "";
            }
        }

        return ret.toString();
    }

    private void add(String s1, String s2) {
        int n = Math.min(s1.length(), s2.length());
        int i = 0;
        for (; i < n; i++) {
            char c1 = s1.charAt(i), c2 = s2.charAt(i);
            if (c1 != c2) {
                if (!edges.containsKey(c1)) {
                    edges.put(c1, new HashSet<>());
                }
                if (!edges.get(c1).contains(c2)) {
                    edges.get(c1).add(c2);
                    // 更新c2入度值
                    in.put(c2, in.get(c2) + 1);
                }
                break;
            }
        }
        if (i == s2.length() && i < s1.length()) {
            check = true;
        }
    }
}

class Solution2 {
    public int tribonacci(int n) {
        if (n == 0) return 0;
        if (n == 1 || n == 2) return 1;
        int a = 0, b = 1, c = 1, s = 0;
        for (int i = 0; i < n - 3; i++) {
            s = a + b + c;
            a = b;
            b = c;
            c = s;
            System.out.println(s);
        }
        return s;
    }
}

class Solution3 {
    public int tribonacci(int n) {
        if (n == 0) return 0;
        if (n == 1 || n == 2) return 1;
        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = dp[2] = 1;
        for (int i = 3; i < n + 1; i++) {
            dp[i] = dp[i - 2] + dp[i - 1] + dp[i - 3];
        }
        return dp[n];
    }
}

class Solution4 {
    int[] step;
    int mod = (int) 1e9 + 7;

    public int waysToStep(int n) {
        if (n == 1) return 1;
        if (n == 2) return 2;
        if (n == 3) return 4;
        step = new int[n + 1];
        step[0] = 0;
        step[1] = 1;
        step[2] = 2;
        step[3] = 4;
        for (int i = 4; i < n + 1; i++) {
            step[i] = ((step[i - 3] + step[i - 2]) % mod + step[i - 1]) % mod;
        }
        return step[n];
    }
}

class Solution5 {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n + 1];
        for (int i = 3; i < n + 1; i++) {
            dp[i] = Math.min(dp[i - 2] + cost[i - 2], dp[i - 1] + cost[i - 1]);
        }
        return dp[n];
    }
}

class Solution6 {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n];
        dp[n - 1] = cost[n - 1];
        dp[n - 2] = cost[n - 2];
        for (int i = n - 3; i >= 0; i--) {
            dp[i] = Math.min(dp[i + 1] + cost[i], dp[i + 2] + cost[i]);
        }
        return dp[0];
    }
}

class Solution {
    List<List<Integer>> ret = new ArrayList<>();

    public void findPath(int[][] graph, int i, List<Integer> list) {
        int n = graph.length;
        list.add(i);
        if (i == n - 1) {
            ret.add(list);
            return;
        }
        for (int j = 0; j < graph[i].length; j++) {
            int k = graph[i][j]; //与i节点联通的下一个节点
            if (k != 0) {
                findPath(graph, k, list);
            }
        }
    }

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        for (int i = 0; i < graph[0].length; i++) {
            int k = graph[0][i];
            if (k != 0) {
                List<Integer> list = new ArrayList<>();
                list.add(0);
                findPath(graph, k, list);
            }
        }
        return ret;
    }
}


public class Test {
    public static void main(String[] args) {
        int[][] graph = {{1,2},{3},{3},{}};
        new Solution().allPathsSourceTarget(graph);
    }
}
